package com.controller;

import com.config.MinioClientConfig;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.util.Calendar;

/**
 * @ClassName : Test03_File
 * @Auther : acerolan@foxmail.com
 * @Data : 2024/7/18 14:12
 * @Description :
 */
@RestController
public class Test03_File {

    @Autowired
    MinioClient minioClient;

    @Autowired
    MinioClientConfig minioClientConfig;

    @PostMapping("/upload")
    @CrossOrigin(methods = {RequestMethod.POST}, value = "*")
    public String upload(@RequestParam("file")MultipartFile file) throws Exception {

        String bucket = minioClientConfig.getBucket();
        String filepath = this.getFilepathDate() + file.getOriginalFilename();

        this.minioClient.putObject(
            PutObjectArgs.builder()
            .bucket(bucket)
            .object(filepath) // 文件的路径
            .stream(file.getInputStream(), -1L, 10485760L)
            .build()
        );

        return filepath;
    }

    public String getFilepathDate() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int date = calendar.get(Calendar.DATE);

        return year + "-" + month + "-" + date + "/";
    }

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int date = calendar.get(Calendar.DATE);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);
        int sec = calendar.get(Calendar.SECOND);

        System.out.println(year);
        System.out.println(month);
        System.out.println(date);
        System.out.println(week);
        System.out.println(hour);
        System.out.println(min);
        System.out.println(sec);
    }

}
